//
//  SCB_Today_List_Detail_ViewController.m
//  Time
//
//  Created by dllo on 16/7/28.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SCB_Today_List_Detail_ViewController.h"
#import "TodayContent.h"
#import "Today_debris_ContentTableViewCell.h"

@interface SCB_Today_List_Detail_ViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 *  返回按钮
 */
@property (nonatomic,retain) UIButton *backButton;
/**
 *  model数组
 */
@property (nonatomic,retain) NSMutableArray *modelArray;
/**
 *  tableView
 */
@property (nonatomic,retain) UITableView *debrisTableView;
/**
 *  用来判断上拉刷新还是下拉加载
 */
@property (nonatomic,assign) BOOL isDown;
/**
 *  body体的值
 */
@property (nonatomic,assign) NSInteger debrisPageSize;
@end

@implementation SCB_Today_List_Detail_ViewController

-(void)dealloc{
    [_backButton release];
    [_tag release];
    [_modelArray release];
    [_debrisTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArray = [NSMutableArray arrayWithCapacity:5];
    
    [self createDebrisTableView];
    [self createToolView];
    [self handleDate];
}

-(void)handleDate {
    
    if (self.isDown){
        self.debrisPageSize = 10;
    } else {
        self.debrisPageSize = self.debrisPageSize + 10;
    }
    
    
    NSString *urlStr = @"http://api.pianke.me/version4.0/timeline/listByTag.php?sig=B573321DD0081433439269DD1A6B7551";
    NSString *body = [NSString stringWithFormat:@"pageSize=%ld&tag=%@",self.debrisPageSize,self.tag];
    NSDictionary *header = @{@"Authorization":HEADER};
    
    [SCBNetTool POST:urlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
//        if (self.isDown){
            [self.modelArray removeAllObjects];
            for (NSDictionary *dic in array) {
                TodayContent *model = [[TodayContent alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArray addObject:model];
            }
            // 在这刷新一次数据
            [self.debrisTableView reloadData];
//        } else {
//            for (NSDictionary *dic in array) {
//                TodayContent *model = [[TodayContent alloc] init];
//                [model setValuesForKeysWithDictionary:dic];
//                [self.modelArray addObject:model];
//            }
//            // 在这刷新一次数据
//            [self.debrisTableView reloadData];
//        }
    } withFaolure:^(NSError *error) {
        NSLog(@"碎片ListDetail抓取数据失败%@",error);
    }];
    
}

-(void)createToolView{
    UIView *toolView = [[UIView alloc] init];
    [self.view addSubview:toolView];
    toolView.backgroundColor = [UIColor whiteColor];
    [toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
        make.height.mas_equalTo(SWIDTH * 0.2);
    }];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:self.backButton];
    [self.backButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(toolView.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setImage:[UIImage imageNamed:@"向左.png"] forState:UIControlStateNormal];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = self.tag;
    [toolView addSubview:label];
    [label mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backButton.mas_right).offset(10);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(300);
    }];

}

-(void)backButtonAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createDebrisTableView{
    self.debrisTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - SWIDTH * 0.2) style:UITableViewStyleGrouped];
    [self.view addSubview:self.debrisTableView];
    self.debrisTableView.delegate = self;
    self.debrisTableView.dataSource = self;
    
    self.debrisTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.debrisTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isDown = YES;
        [self handleDate];
        [self.debrisTableView.mj_header endRefreshing];
    }];
    
    self.debrisTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.isDown = NO;
        [self handleDate];
        [self.debrisTableView.mj_footer endRefreshing];
    }];
    
    
    [self.debrisTableView registerClass:[Today_debris_ContentTableViewCell class] forCellReuseIdentifier:@"today_debris_Cell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    SCB_Today_Debris_ContentModel *model = [self.debrisModelArray objectAtIndex:indexPath.section];
    
        Today_debris_ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_debris_Cell" forIndexPath:indexPath];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TodayContent *model = [self.modelArray objectAtIndex:indexPath.section];
    
        if (![model.coverimg isEqualToString:@""]){
            
            return 50 + SWIDTH + [Tools getTextHeight:model.text withWidth:SWIDTH - 20 withFontSize:15.0] + 70;
            
        } else {
            
            return 50 + [Tools getTextHeight:model.text withWidth:SWIDTH - 20 withFontSize:15.0] + 70;
            
        }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
